#include "replacedialog.h"
#include "ui_replacedialog.h"
#include <QMessageBox>

ReplaceDialog::ReplaceDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ReplaceDialog)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    initial();
}

ReplaceDialog::~ReplaceDialog()
{
    delete ui;
}

void ReplaceDialog::initial()
{
    ui->loop->setCheckState(Qt::Checked);
    ui->findLineEdit->setMaxLength(20);
    ui->rePlaceLineEdit->setMaxLength(20);
    connect(ui->findLineEdit,&QLineEdit::textChanged,this,&ReplaceDialog::btnEnable);
}



void ReplaceDialog::on_findNextBtn_clicked()
{
    if(ui->findLineEdit->text().isEmpty())
    {
        return;
    }
    bool isCaseSenitive = (ui->caseSenitive->isChecked())?true:false;
    bool isLoop = (ui->loop->isChecked())?true:false;
    bool direction = true;//这里的查找默认为向上
    emit searchNextText(ui->findLineEdit->text(),isCaseSenitive,isLoop,direction);
}

void ReplaceDialog::on_replaceBtn_clicked()
{
    if(ui->rePlaceLineEdit->text().isEmpty())
    {
        QMessageBox::information(this,tr("HanL的记事本"),tr("替换字符为空，请重新输入"),QMessageBox::Ok);
        return;
    }
    bool isCaseSenitive = (ui->caseSenitive->isChecked())?true:false;
    bool isLoop = (ui->loop->isChecked())?true:false;
    bool direction = true;//这里查找的方向默认为向上
    emit replaceText(ui->findLineEdit->text(),ui->rePlaceLineEdit->text(),isCaseSenitive,isLoop,direction);

}

void ReplaceDialog::on_closeBtn_clicked()
{
    this->close();
}

void ReplaceDialog::on_cancelBtn_clicked()
{
    this->close();
}

void ReplaceDialog::btnEnable()
{
    ui->findNextBtn->setEnabled(!ui->findLineEdit->text().isEmpty());
    ui->replaceBtn->setEnabled(!ui->findLineEdit->text().isEmpty());
    ui->allReplaceBtn->setEnabled(!ui->findLineEdit->text().isEmpty());
}

void ReplaceDialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton && ui->frame->geometry().contains(event->pos()))
    {
        m_dragPos = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void ReplaceDialog::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos() - m_dragPos);
        event->accept();
    }
}

